package com.jinzhi.jzweb.controller;


import java.text.ParseException;
import java.util.*;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.jzapi.utils.CommonUtil;
import com.jinzhi.jzweb.domain.cond.InformationImportUserCond;
import com.jinzhi.jzweb.domain.vo.InformationImportUserVo;
import com.jinzhi.jzweb.domain.vo.InformationImportVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.InformationImportUserDO;
import com.jinzhi.jzweb.service.InformationImportUserService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2024-05-15 17:22:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/informationImportUser")
public class InformationImportUserController extends BaseController {
	@Autowired
	private InformationImportUserService informationImportUserService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:informationImportUser:informationImportUser")
	String InformationImportUser(){
	    return "jzweb/informationImportUser/informationImportUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:informationImportUser:informationImportUser")
	public Result<Page<InformationImportUserDO>> list(InformationImportUserDO informationImportUserDTO){
		// 查询列表数据
		Page<InformationImportUserDO> page = informationImportUserService.selectPage(getPage(InformationImportUserDO.class),
					informationImportUserService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				InformationImportUserDO obj = (InformationImportUserDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:informationImportUser:add")
	String add(){
	    return "jzweb/informationImportUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:informationImportUser:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InformationImportUserDO informationImportUser = informationImportUserService.selectById(id);
		model.addAttribute("informationImportUser", informationImportUser);
	    return "jzweb/informationImportUser/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:informationImportUser:add")
	public Result<String> save( InformationImportUserDO informationImportUser){
		informationImportUserService.insert(informationImportUser);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:informationImportUser:edit")
	public Result<String>  update( InformationImportUserDO informationImportUser){
		boolean update = informationImportUserService.updateById(informationImportUser);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:informationImportUser:remove")
	public Result<String>  remove( Long id){
		informationImportUserService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:informationImportUser:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		informationImportUserService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	/**
	 *  信息列表查询
	 * @return
	 */
	@GetMapping("/selImportUserList")
	@ResponseBody
	public Result<Page<InformationImportUserDO>> selImportUserList(@RequestParam(value = "name",required = false) String name,
														   @RequestParam(value = "mobile",required = false) String mobile,
														   @RequestParam("current")int current,
														   @RequestParam("size")int size,
															   @RequestParam(value = "createTime",required = false) String createTime,
														   @RequestParam(value = "endTime",required = false)String endTime,
															@RequestParam(value = "personType",required = false) Integer personType,
														   @RequestParam(value = "checkStatus",required = false)Integer checkStatus,
														   @RequestParam(value = "examTime",required = false)String examTime,
														   @RequestParam(value = "declaredCareers",required = false)String declaredCareers
	)  {

		return informationImportUserService.selImportUserList(name,mobile,current,size,createTime,endTime,personType,checkStatus,examTime,declaredCareers);
	}

	/**
	 * 信息详情
	 * @return
	 */
	@GetMapping("/getInfo")
	@ResponseBody
	public Result<?> getInfo(@RequestParam("id")Long id)  {
		InformationImportUserVo vo=informationImportUserService.getInfo1(id);
		return Result.ok(vo);

	}

	/**
	 * 审核
	 * @return
	 */
	@GetMapping("/checkImportUser")
	@ResponseBody
	public Result<String> checkImportUser(@RequestParam("id")Long id,@RequestParam("state")Integer state,
									 @RequestParam("reason")String reason){
		InformationImportUserDO informationImportUserDO = informationImportUserService.selectById(id);
		informationImportUserDO.setCheckStatus(state);
		informationImportUserDO.setReason(reason);
		informationImportUserDO.setAuditsTime(new Date());
		informationImportUserService.updateById(informationImportUserDO);
		return Result.ok();
	}

	/**
	 * 修改信息录入信息
	 * @return
	 */
	@ResponseBody
	@PostMapping("/updateImportUser")
	public Result<?> updateImportUser(@RequestBody InformationImportUserCond cond)throws ParseException {
		//检查电话
//		boolean b1 = CommonUtil.checkTel(cond.getMobile());
//		if (!b1){
//			return Result.fail("手机号码不正确");
//		}
		//检查身份证
		boolean validID = CommonUtil.isValidID(cond.getIdcardNumber());
		if (!validID){
			return Result.fail("身份证号码不正确");
		}

		return Result.ok(informationImportUserService.updateImportUser1(cond));
	}

	/**
	 * 新增信息录入信息
	 * @return
	 */
	@ResponseBody
	@PostMapping("/addImportUser")
	public Result<?> addImportUser(@RequestBody InformationImportUserCond cond)throws ParseException {
		//检查电话
//		boolean b1 = CommonUtil.checkTel(cond.getMobile());
//		if (!b1){
//			return Result.fail("手机号码不正确");
//		}
		//检查身份证
		boolean validID = CommonUtil.isValidID(cond.getIdcardNumber());
		if (!validID){
			return Result.fail("身份证号码不正确");
		}
		return Result.ok(informationImportUserService.saveSignUserInfo1(cond));
	}

	/**
	 * 导出信息录入数据
	 *
	 * @param response
	 */
	@Log("导出信息录入数据")
	@GetMapping("/downloadImportUser")
	@ResponseBody
	public void downloadImportUser(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "ids",required = false) List<Long> ids) {
		int pageSize = 1000; // 每页查询的数据量
		int pageNum = 1; // 起始页码

		List<InformationImportVo> list = new LinkedList<>();
		boolean hasMoreData = true;

		try {
			while (hasMoreData) {
				Wrapper<InformationImportUserDO>  wrapper=new EntityWrapper<>();
				wrapper.in("id",ids);
				Page<InformationImportUserDO> page = new Page<>(pageNum, pageSize);
				// 分页查询数据
				Page<InformationImportUserDO> pageInfo = informationImportUserService.selectPage(page, wrapper);
				List<InformationImportUserDO> informationImportUserDOS = pageInfo.getRecords();

				if (informationImportUserDOS != null && !informationImportUserDOS.isEmpty()) {
					for (InformationImportUserDO informationImportUserDO : informationImportUserDOS) {
						InformationImportVo informationImportVo = getInformationImportVo(informationImportUserDO);
						list.add(informationImportVo);
					}
				}

				// 如果当前页的记录数小于pageSize，说明已经是最后一页
				if (informationImportUserDOS.size() < pageSize) {
					hasMoreData = false;
				} else {
					pageNum++; // 继续下一页
				}
			}

			//导出操作
			EasyPoiUtil.exportExcel(list, "信息录入人员信息", "信息录入人员信息", InformationImportVo.class,  "信息录入人员信息表", response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
	}


	private static InformationImportVo getInformationImportVo(InformationImportUserDO informationImportUserDO) {
		InformationImportVo informationImportVo = new InformationImportVo();
		informationImportVo.setName(informationImportUserDO.getName());
		informationImportVo.setIdcardNumber(informationImportUserDO.getIdcardNumber());
		String idcardAddress = informationImportUserDO.getIdcardAddress();
		int province = idcardAddress.indexOf("省"); // 获取"省"的索引位置
		if (province != -1) { // 确保找到了关键词
			String result = idcardAddress.substring(0, province + 1); // 截取从开始到"省"结束的内容
			informationImportVo.setProvince(result);
		}
		int city = idcardAddress.indexOf("市"); // 获取"市"的索引位置
		if (city != -1) { // 确保找到了关键词
			String result = idcardAddress.substring(province+1, city + 1); // 截取从“省”开始到"市"结束的内容
			informationImportVo.setCity(result);
		}
		informationImportVo.setWorkTime(informationImportUserDO.getWorkTime());
		return informationImportVo;
	}

}
